/**
 * VimL循环控制结构示例
 * 
 * 本示例展示了VimL中各种循环控制结构的使用方法，包括：
 * 1. for循环 - 遍历列表和数组
 * 2. while循环 - 条件控制循环
 * 3. 嵌套循环 - 多层循环结构
 * 4. 循环中的条件判断
 * 
 * 这些是VimL编程中的基础控制结构，掌握它们对于编写复杂程序至关重要。
 * 
 * 编译和运行：
 * mkdir -p build && cd build
 * cmake .. && ninja
 * ./bin/loop_control_structures_example
 * 
 * 作者：VimL虚拟机项目组
 * 日期：2025-09-27
 */

#include "viml/compiler.h"
#include "viml/vm.h"
#include <iostream>

using namespace viml;

void print_header(const std::string& title) {
    std::cout << "\n" << std::string(60, '=') << std::endl;
    std::cout << " " << title << std::endl;
    std::cout << std::string(60, '=') << std::endl;
}

int main() {
    try {
        Compiler compiler;
        VM vm;
        
        print_header("VimL循环控制结构示例");
        
        // ========== for循环示例 ==========
        std::string for_loop_examples = R"(
echo "=== For循环示例 ==="

" 1. 遍历数字列表
echo "1. 遍历数字列表："
let numbers = [1, 2, 3, 4, 5]
for num in numbers
    echo "数字:", num
endfor

" 2. 遍历字符串列表
echo "2. 遍历字符串列表："
let fruits = ["苹果", "香蕉", "橘子"]
for fruit in fruits
    echo "水果:", fruit
endfor

" 3. 遍历空列表（测试边界情况）
echo "3. 遍历空列表："
let empty_list = []
echo "开始遍历"
for item in empty_list
    echo "这行不应该出现"
endfor
echo "遍历结束"
)";
        
        std::cout << "执行for循环示例..." << std::endl;
        auto bytecode1 = compiler.compile(for_loop_examples, "for_loops.vim");
        vm.execute(bytecode1);
        
        // ========== while循环示例 ==========
        compiler.reset();
        vm.reset();
        
        std::string while_loop_examples = R"(
echo "=== While循环示例 ==="

" 1. 基本计数循环
echo "1. 基本计数循环："
let counter = 1
while counter <= 5
    echo "计数:", counter
    let counter = counter + 1
endwhile

" 2. 累加循环
echo "2. 累加循环："
let sum = 0
let i = 1
while i <= 10
    let sum = sum + i
    let i = i + 1
endwhile
echo "1到10的和:", sum

" 3. 条件控制循环
echo "3. 条件控制循环："
let value = 1
while value < 20
    echo "当前值:", value
    let value = value * 2
endwhile
)";
        
        std::cout << "\n执行while循环示例..." << std::endl;
        auto bytecode2 = compiler.compile(while_loop_examples, "while_loops.vim");
        vm.execute(bytecode2);
        
        // ========== 嵌套循环示例 ==========
        compiler.reset();
        vm.reset();
        
        std::string nested_loop_examples = R"(
echo "=== 嵌套循环示例 ==="

" 1. for循环嵌套
echo "1. For循环嵌套 - 打印乘法表："
let rows = [1, 2, 3]
let cols = [1, 2, 3]
for row in rows
    for col in cols
        let result = row * col
        echo row, "x", col, "=", result
    endfor
endfor

" 2. while循环嵌套
echo "2. While循环嵌套 - 矩阵遍历："
let outer_i = 1
while outer_i <= 2
    let inner_j = 1
    while inner_j <= 3
        echo "位置 [" .. outer_i .. "," .. inner_j .. "]"
        let inner_j = inner_j + 1
    endwhile
    let outer_i = outer_i + 1
endwhile
)";
        
        std::cout << "\n执行嵌套循环示例..." << std::endl;
        auto bytecode3 = compiler.compile(nested_loop_examples, "nested_loops.vim");
        vm.execute(bytecode3);
        
        // ========== 循环中的条件判断 ==========
        compiler.reset();
        vm.reset();
        
        std::string conditional_loops = R"(
echo "=== 循环中的条件判断 ==="

" 1. for循环中的if条件
echo "1. For循环中筛选偶数："
let all_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in all_numbers
    " 简化的偶数判断
    let half = number / 2
    let doubled = half * 2
    if doubled == number
        echo number, "是偶数"
    endif
endfor

" 2. while循环中的复杂条件
echo "2. While循环中的条件处理："
let index = 1
let found_target = 0
let target_list = [5, 10, 15, 20, 25]

while index <= len(target_list)
    let current = target_list[index - 1]
    echo "检查第", index, "个元素:", current
    
    if current == 15
        echo "找到目标值 15!"
        let found_target = 1
    endif
    
    let index = index + 1
endwhile

if found_target == 1
    echo "搜索成功"
else
    echo "未找到目标"
endif
)";
        
        std::cout << "\n执行条件循环示例..." << std::endl;
        auto bytecode4 = compiler.compile(conditional_loops, "conditional_loops.vim");
        vm.execute(bytecode4);
        
        print_header("循环控制结构总结");
        
        std::cout << "📚 循环结构学习要点：" << std::endl;
        std::cout << "\n1. For循环特点：" << std::endl;
        std::cout << "   ✓ 适合遍历已知集合（列表、数组）" << std::endl;
        std::cout << "   ✓ 语法简洁：for item in items" << std::endl;
        std::cout << "   ✓ 自动处理循环变量" << std::endl;
        std::cout << "   📝 推荐用于数据遍历" << std::endl;
        
        std::cout << "\n2. While循环特点：" << std::endl;
        std::cout << "   ✓ 基于条件控制循环" << std::endl;
        std::cout << "   ✓ 灵活性高，适合复杂逻辑" << std::endl;
        std::cout << "   ⚠️  需要手动管理循环变量" << std::endl;
        std::cout << "   📝 推荐用于条件驱动的循环" << std::endl;
        
        std::cout << "\n3. 嵌套循环注意事项：" << std::endl;
        std::cout << "   ⚠️  注意变量作用域" << std::endl;
        std::cout << "   ⚠️  避免过深的嵌套层次" << std::endl;
        std::cout << "   ✓ 适合处理多维数据" << std::endl;
        std::cout << "   📝 常用于矩阵、表格操作" << std::endl;
        
        std::cout << "\n4. 性能考虑：" << std::endl;
        std::cout << "   • 避免在循环中进行重复的复杂计算" << std::endl;
        std::cout << "   • 合理使用条件判断减少不必要的操作" << std::endl;
        std::cout << "   • 考虑循环的时间复杂度" << std::endl;
        
        std::cout << "\n🎯 实践建议：" << std::endl;
        std::cout << "• 优先使用for循环处理已知集合" << std::endl;
        std::cout << "• 使用while循环处理复杂条件逻辑" << std::endl;
        std::cout << "• 在嵌套循环中保持代码清晰" << std::endl;
        std::cout << "• 合理使用条件判断优化性能" << std::endl;
        
        std::cout << "\n🎉 循环控制结构示例演示完成！" << std::endl;
        
        return 0;
        
    } catch (const std::exception& e) {
        std::cout << "\n❌ 循环控制结构示例执行失败: " << e.what() << std::endl;
        return 1;
    }
}